package com.wyd.jwpkglxt_ssm.controller;

import com.wyd.jwpkglxt_ssm.entity.Department;
import com.wyd.jwpkglxt_ssm.entity.ProfessionalTitle;
import com.wyd.jwpkglxt_ssm.service.ProfessionalTitleService;
import com.wyd.jwpkglxt_ssm.service.PtDepartmentService;
import com.wyd.jwpkglxt_ssm.util.ResultCode;
import com.wyd.jwpkglxt_ssm.util.ResultUtil;
import com.wyd.jwpkglxt_ssm.vo.ProfessionalTitleVo;
import org.springframework.web.bind.annotation.*;

import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * (ProfessionalTitle)表控制层
 *
 * @author wyd
 * @since 2021-08-07 21:26:54
 */
@RestController
@RequestMapping("professionalTitle")
public class ProfessionalTitleController {
    /**
     * 服务对象
     */
    @Autowired
    private ProfessionalTitleService professionalTitleService;

    @Autowired
    private PtDepartmentService ptDepartmentService;
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ProfessionalTitle selectOne(Integer id) {
        return this.professionalTitleService.queryById(id);
    }

    @GetMapping("professionalTitle/page")
    public ResultUtil getAllProfessionalTitleVoByPage(Integer pageNum, Integer pageSize,
                                                      @RequestParam(required = false)String realName,@RequestParam(required = false)String title,
                                                      @RequestParam(required = false)Integer did) {
        ProfessionalTitleVo professionalTitleVo = new ProfessionalTitleVo().setRealName(realName).setTitle(title).setDid(did);
        return ResultUtil.ok().data("pageInfo",this.professionalTitleService.queryAllByPage(pageNum,pageSize,professionalTitleVo));
    }

    @PostMapping("professionalTitle")
    public ResultUtil addProfessionalTitleVo(@RequestBody ProfessionalTitleVo professionalTitleVo){
        if(!Objects.isNull(professionalTitleVo)){
            Boolean insert = this.professionalTitleService.insert(professionalTitleVo);
            if (insert){
                return ResultUtil.ok();
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @PutMapping("professionalTitle")
    public ResultUtil updateProfessionalTitleVo(@RequestBody ProfessionalTitleVo professionalTitleVo){
        System.out.println(professionalTitleVo);
        if(!Objects.isNull(professionalTitleVo)){
            Boolean update = this.professionalTitleService.update(professionalTitleVo);
            if(update){
                return ResultUtil.ok();
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }

    @DeleteMapping("professionalTitle/{professionalTitleId}/{ptDepartmentId}")
    public ResultUtil deleteProfessionalTitleVoById(@PathVariable("professionalTitleId")String professionalTitleIds,@PathVariable("ptDepartmentId")String ptDepartmentIds){
        Boolean deleteProfessionalTitle = this.professionalTitleService.deleteProfessionalTitle(professionalTitleIds);
        if(deleteProfessionalTitle){
            Boolean deletePtDepartment = this.ptDepartmentService.deletePtDepartment(ptDepartmentIds);
            if(deletePtDepartment){
                return ResultUtil.ok();
            }
        }
        return ResultUtil.error(ResultCode.UNKNOWN_ERROR);
    }


    @GetMapping("professionalTitle")
    public ResultUtil getAllProfessionalTitle(ProfessionalTitleVo professionalTitleVo){
        return ResultUtil.ok().data("professionalTitles",this.professionalTitleService.queryAllWithProfessionalTitleVo(professionalTitleVo));
    }
}